/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.eweboffice;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.eweboffice.Document;

/**
 * @author CSJ (email:Cao.shengjie777@gmail.com)
 * @since 2013-04-09
 */
@Service("documentService")
public class DocumentServiceImpl implements DocumentService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Document
     ****************************************/
    public void insert(Document document) throws Exception {
        iDataSet4iBatis.insert(document);
    }

    /****************************************
     * Update a instace of Document
     ****************************************/
    public void update(Document document) throws Exception {
        iDataSet4iBatis.update(document);
    }

    /****************************************
     * Delete a instace of Document
     ****************************************/
    public void delete(Document document) throws Exception {
        iDataSet4iBatis.delete(document);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Document document) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Document document) throws Exception {
        String statementId = document.getStatmentId();
        String mainId = document.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> documentList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Document model = new Document();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            documentList.add(model);
        }
        iDataSet4iBatis.batchDelete(documentList);
    }

    /****************************************
     * Query instaces of Document
     ****************************************/
    public List<BaseModel> queryAsList(Document document) throws Exception {
        String queryListStatement = "";
        if (document.isPaging()) {
            queryListStatement = document.getStatmentId();
            document.setStatmentId("Document.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(document);
            document.setTotalCount(baseModel.getTotalCount());
            document.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(document);
    }

    /****************************************
     * Query a instaces of Document
     ****************************************/
    public BaseModel queryAsObject(Document document) throws Exception {
        return iDataSet4iBatis.queryAsObject(document);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
